#!/bin/sh -e
# This script is used to generate the tarballs of xapian for xappy.
#
# These tarballs are based on SVN HEAD, but with the changes from two branches
# merged in.  Some of the contents of these branches may be merged into HEAD
# shortly, but other changes on the branches can't easily be merged to HEAD
# during the 1.0.x release series, so will be merged after the 1.0.x series has
# been moved into maintenance mode, and HEAD is being used to work towards the
# 1.1 series.
#
# The script depends on various directories being set up, and is probably only
# usable on my machine without a fair bit of work first.

# The directories used are:
#
#  $basedir/head/trunk: a checkout of HEAD, at the latest revision.
#
#  $xappylibdir: a checkout of xappy/trunk.

rev="1.1.2"
trunk_rev="13268"
matchspy_branchpoint="13246"
clustering_branchpoint="12664"
geospatial_branchpoint="13235"
coloursim_branchpoint="13268"

if [ -e "$HOME/xappy" ]; then
basedir="$HOME/xappy/maketarballs"
xappylibdir="$HOME/xappy/libs/"
else
basedir="$HOME/src/xappy/maketarballs"
xappylibdir="$HOME/src/xappy/libs/"
fi

mkdir -p ${basedir}

echo "Updating head"
mkdir -p ${basedir}/head
cd ${basedir}/head
svn co -q svn://svn.xapian.org/xapian/trunk -r "$trunk_rev"
cd trunk
head_rev=`svn info | grep Revision | cut -f 2 -d ' '`

echo "Getting branch patches ..."
echo " ... matchspy"
matchspy_rev=`svn info svn://svn.xapian.org/xapian/branches/matchspy | grep "Last Changed Rev" | cut -f 4 -d ' '`
if [ ! -e $basedir/matchspy_changes_${matchspy_branchpoint}_${matchspy_rev}.patch ] ; then
svn diff --diff-cmd diff -x "-Nur --ignore-matching-lines='\$Author'" \
	"svn://svn.xapian.org/xapian/trunk@$matchspy_branchpoint" \
	"svn://svn.xapian.org/xapian/branches/matchspy" \
| filterdiff -x '*/ChangeLog' -x '*/NEWS' -x '*/docs/index.html' \
  >$basedir/matchspy_changes_${matchspy_branchpoint}_${matchspy_rev}.patch
fi

echo " ... clustering"
clustering_rev=`svn info svn://svn.xapian.org/xapian/branches/clustering | grep "Last Changed Rev" | cut -f 4 -d ' '`
if [ ! -e $basedir/clustering_changes_${clustering_branchpoint}_${clustering_rev}.patch ] ; then
svn diff --diff-cmd diff -x "-Nur --ignore-matching-lines='\$Author'" \
	"svn://svn.xapian.org/xapian/trunk@$clustering_branchpoint" \
	"svn://svn.xapian.org/xapian/branches/clustering" \
| filterdiff -x '*/ChangeLog' -x '*/NEWS' -x '*/docs/index.html' \
  >$basedir/clustering_changes_${clustering_branchpoint}_${clustering_rev}.patch
fi

echo " ... geospatial"
geospatial_rev=`svn info svn://svn.xapian.org/xapian/branches/geospatial | grep "Last Changed Rev" | cut -f 4 -d ' '`
if [ ! -e $basedir/geospatial_changes_${geospatial_branchpoint}_${geospatial_rev}.patch ] ; then
svn diff --diff-cmd diff -x "-Nur --ignore-matching-lines='\$Author'" \
	"svn://svn.xapian.org/xapian/trunk@$geospatial_branchpoint" \
	"svn://svn.xapian.org/xapian/branches/geospatial" \
| filterdiff -x '*/ChangeLog' -x '*/NEWS' -x '*/docs/index.html' \
  >$basedir/geospatial_changes_${geospatial_branchpoint}_${geospatial_rev}.patch
fi

echo " ... coloursim"
coloursim_rev=`svn info svn://svn.xapian.org/xapian/branches/coloursim | grep "Last Changed Rev" | cut -f 4 -d ' '`
if [ ! -e $basedir/coloursim_changes_${coloursim_branchpoint}_${coloursim_rev}.patch ] ; then
svn diff --diff-cmd diff -x "-Nur --ignore-matching-lines='\$Author'" \
	"svn://svn.xapian.org/xapian/trunk@$coloursim_branchpoint" \
	"svn://svn.xapian.org/xapian/branches/coloursim" \
| filterdiff -x '*/ChangeLog' -x '*/NEWS' -x '*/docs/index.html' \
  >$basedir/coloursim_changes_${coloursim_branchpoint}_${coloursim_rev}.patch
fi

echo "Exporting clean tree"
rm -rf $basedir/head/trunk_export
svn export $basedir/head/trunk $basedir/head/trunk_export
if [ ! -d $basedir/head/trunk_export/swig ] ; then
  svn export $basedir/head/trunk/swig $basedir/head/trunk_export/swig
fi

# Apply the patches to HEAD
cd $basedir/head/trunk_export
echo "Applying patches from matchspy branch"
patch -p0 < $basedir/matchspy_changes_${matchspy_branchpoint}_${matchspy_rev}.patch
echo "Applying patches from clustering branch"
patch -p0 < $basedir/clustering_changes_${clustering_branchpoint}_${clustering_rev}.patch
echo "Applying patches from geospatial branch"
patch -p0 < $basedir/geospatial_changes_${geospatial_branchpoint}_${geospatial_rev}.patch
echo "Applying patches from coloursim branch"
patch -p0 < $basedir/coloursim_changes_${coloursim_branchpoint}_${coloursim_rev}.patch

# Make tarballs
echo "Building tarballs"
cd $basedir/head/trunk_export
rm -rf search-xapian
rm -rf xapian-applications
./bootstrap
./configure
(cd xapian-core && make && make dist)
(cd swig && make)
(cd xapian-bindings && make && make dist)

# Copy the tarballs into xappy
echo "Repacking tarballs"
mkdir -p $xappylibdir
cd $xappylibdir
mkdir -p tmp
cd tmp

rm -rf xapian-core-* xapian-bindings-*
cd $basedir/head/trunk_export
cp xapian-core/xapian-core-${rev}.tar.gz $xappylibdir/tmp/
cp xapian-bindings/xapian-bindings-${rev}.tar.gz $xappylibdir/tmp/
cd $xappylibdir/tmp
tar zxf $xappylibdir/tmp/xapian-core-${rev}.tar.gz
tar zxf $xappylibdir/tmp/xapian-bindings-${rev}.tar.gz
mv xapian-core-${rev} xapian-core
mv xapian-bindings-${rev} xapian-bindings
tar -H ustar -zcf xapian-core-${head_rev}.tgz xapian-core
tar -H ustar -zcf xapian-bindings-${head_rev}.tgz xapian-bindings
rm -r xapian-core xapian-bindings
rm xapian-core-${rev}.tar.gz
rm xapian-bindings-${rev}.tar.gz

cd $xappylibdir
mv tmp/xapian-core-${head_rev}.tgz .
mv tmp/xapian-bindings-${head_rev}.tgz .

rmdir tmp

# Make a tarball of the windows build files
cd $basedir/head/trunk_export/xapian-maintainer-tools/
tar -H ustar -zcf win32msvc-${head_rev}.tgz win32msvc
cp win32msvc-${head_rev}.tgz $xappylibdir/
